home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ident / tools / tcplist-1.1.shar / kvm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-14  |  2.2 KB  |  141 lines

  1. /*
  2. ** kvm.c            A set of functions emulating KVM for machines without them.
  3. **
  4. ** This code is in the public domain and may be used freely by anyone
  5. ** who wants to.
  6. **
  7. ** Last update: 1 July 1992
  8. **
  9. ** Author: Peter Eriksson <pen@lysator.liu.se>
  10. */
  11.  
  12. #ifndef HAVE_KVM
  13.  
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <nlist.h>
  17. #if defined(IRIX) || defined(MIPS)
  18. #include <sys/sbd.h>
  19. #endif
  20. #include "kvm.h"
  21. #include "paths.h"
  22.  
  23.  
  24. extern void *malloc();
  25.  
  26.  
  27. kvm_t *kvm_open(namelist, corefile, swapfile, flag, errstr)
  28.   char *namelist;
  29.   char *corefile;
  30.   char *swapfile;
  31.   int flag;
  32.   char *errstr;
  33. {
  34.   kvm_t *kd;
  35.  
  36.  
  37.   if (!namelist)
  38.     namelist = _PATH_UNIX;
  39.   if (!corefile)
  40.     corefile = _PATH_KMEM;
  41.   
  42.   kd = (kvm_t *) malloc(sizeof(kvm_t));
  43.   if (!kd)
  44.   {
  45.     if (errstr)
  46.       perror(errstr);
  47.     return NULL;
  48.   }
  49.  
  50.   kd->namelist = (char *) malloc(strlen(namelist)+1);
  51.   if (!kd->namelist)
  52.   {
  53.     if (errstr)
  54.       perror(errstr);
  55.     return NULL;
  56.   }
  57.   
  58.   if (!(kd->fd = open(corefile, flag)) < 0)
  59.   {
  60.     if (errstr)
  61.       perror(errstr);
  62.     free(kd->namelist);
  63.     free(kd);
  64.     return NULL;
  65.   }
  66.  
  67.   strcpy(kd->namelist, namelist);
  68.   return kd;
  69. }
  70.  
  71.  
  72. int kvm_close(kd)
  73.   kvm_t *kd;
  74. {
  75.   int code;
  76.   
  77.   code = close(kd->fd);
  78.   free(kd->namelist);
  79.   free(kd);
  80.  
  81.   return code;
  82. }
  83.  
  84.  
  85. /*
  86. ** Extract offsets to the symbols in the 'nl' list. Returns 0 if all found,
  87. ** or else the number of variables that was not found.
  88. */
  89. int kvm_nlist(kd, nl)
  90.   kvm_t *kd;
  91.   struct nlist *nl;
  92. {
  93.   int code;
  94.   int i;
  95.  
  96.  
  97.   code = nlist(kd->namelist, nl);
  98.   if (code != 0)
  99.     return code;
  100.   
  101.   /*
  102.   ** Verify that we got all the needed variables. Needed because some
  103.   ** implementations of nlist() returns 0 although it didn't find all
  104.   ** variables.
  105.   */
  106.   if (code == 0)
  107.   {
  108. #ifdef __convex__
  109.     for (i = 0; nl[i].n_un.n_name && nl[i].n_un.n_name[0]; i++)
  110. #else  
  111.     for (i = 0; nl[i].n_name && nl[i].n_name[0]; i++)
  112. #endif
  113.       if (nl[i].n_type == 0)
  114.     code++;
  115.   }
  116.   
  117.   return code;
  118. }
  119.  
  120.  
  121. /*
  122. ** Get a piece of the kernel memory
  123. */
  124. int kvm_read(kd, addr, buf, len)
  125.   kvm_t *kd;
  126.   long addr;
  127.   char *buf;
  128.   int len;
  129. {
  130. #if defined(IRIX) || defined(MIPS) 
  131.   addr = K0_TO_PHYS(addr);
  132. #endif 
  133.   errno = 0;
  134.   if (lseek(kd->fd, addr, 0) == -1 && errno != 0)
  135.     return -1;
  136.  
  137.   return read(kd->fd, buf, len);
  138. }
  139.  
  140. #endif
  141.